'use strict';

angular.module('fbdApp.modalModule')

.factory('modalService', function($uibModal) {
  // console.log($uibModal, 'modal');
  var tempConfig = {};
  var tempInfo = {};

  var defaultConfig = {
    animation: true,
    templateUrl: 'modalModule/views/modal.html',
    controller: 'modalInstanceCtrl',
    resolve: {
      modalInfo: function() {
        return {}
      }
    },
    // controllerAs: 'vm',
    // template: '<div></div>'
    size: 'lg'
  };

  var defaultInfo = {
    closeButtonText: '取消',
    actionButtonText: '确认',
    headerText: '标题',
    bodyText: '内容',
    buttonDisabled: true
  };

  return {
    showModal: function(customConfig, customInfo) {
      if (!customConfig) customConfig = {};
      if (!customInfo) customInfo = {};

      angular.extend(tempConfig, defaultConfig, customConfig);
      angular.extend(tempInfo, defaultInfo, customInfo);

      // console.log(tempConfig);

      tempConfig.resolve.modalInfo = function() {
        return angular.copy(tempInfo);
      }
      console.log(tempConfig);

      return $uibModal.open(tempConfig);
    },

    // showInfo: function(scope) {
    //   scope.modalInfo = tempInfo;
    //   console.log(tempConfig);
    //   return scope;
    // },

    closeModal: function(scope, result) {
      scope.ok = function() {
        scope.$close(result);
      };
      return scope;
    },

    dismissModal: function(scope, reason) {
      scope.cancel = function() {
        scope.$dismiss(reason);
      };
      return scope;
    }

  }

})

.controller('modalInstanceCtrl', function($scope,
  modalService) {
  modalService.showInfo($scope);
  modalService.closeModal($scope, 'ok');
  modalService.dismissModal($scope, 'cancel');
  // modalService.closeModal($scope, 'oksdfs', 'cancel');


});
